/**
 * This file is part of Wasagent.
 *
 * Wasagent is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Wasagent is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Wasagent. If not, see <http://www.gnu.org/licenses/>.
 * 
 * JmxClientFactory.java: Create a client connection, with proper parameters
 *
 *
 */

package net.wait4it.wasagent.jmx;

import java.io.FileInputStream;

import java.util.Enumeration;
import java.util.Map;
import java.util.Properties;

import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;

/**
 * @author Yann Lambret
 *
 */
public class JmxClientFactory {


	public static AdminClient getInstance(Map<String,String> params) {

		AdminClient client = null;
		Properties props = new Properties();
		

		for (Map.Entry<String, String> entry : params.entrySet()) {
			props.setProperty(entry.getKey(),entry.getValue());
		}

		props.setProperty(AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
		props.setProperty(AdminClient.CONNECTOR_SOAP_REQUEST_TIMEOUT, "10");
		props.setProperty(AdminClient.CONNECTOR_HOST, params.get("hostname"));
		props.setProperty(AdminClient.CONNECTOR_PORT, params.get("port"));
		props.setProperty(AdminClient.USERNAME, params.get("username"));
		props.setProperty(AdminClient.PASSWORD, params.get("password"));
		props.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "true");
		props.setProperty(AdminClient.CONNECTOR_AUTO_ACCEPT_SIGNER,"true");

		try {
			client = AdminClientFactory.createAdminClient(props);
		} catch (Exception e) {
			if (params.containsKey("debug") && params.get("debug").equals("true"))
				e.printStackTrace();
			throw new RuntimeException("Unable to get AdminClient for SOAP connector " + params.get("hostname") + ":" + params.get("port"));
		}
		return client;
	}

}
